package com.qk.management.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.qk.common.PageResult;
import com.qk.entity.Dept;
import com.qk.management.mapper.DeptMapper;
import com.qk.management.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 部门服务实现类
 */
@Service
public class DeptServiceImpl implements DeptService {
    @Autowired
    private DeptMapper deptMapper;

    /**
     * 新增部门
     */
    @Override
    public void addDept(Dept dept) {
        // 设置部门创建时间
        dept.setCreateTime(LocalDateTime.now());
        // 设置部门更新时间
        dept.setUpdateTime(LocalDateTime.now());
        // 调用DAO
        deptMapper.insert(dept);
    }

    /*@Override
    public PageResult<Dept> page(Integer page, Integer pageSize, String name, Integer status) {
        //1.查询满足条件总记录数
        Integer total=deptMapper.count(name, status);
        System.out.println("total:" + total);
        //2.查询当前页数据列表
        Integer offset=(page-1)*pageSize;
        List<Dept> rows=deptMapper.selectByPage(name, status, offset, pageSize);
        System.out.println("DeptServiceImpl  page");
        for (Dept row : rows) {
            System.out.println("rows:" + row);
        }
        //3.数据组装
        return PageResult.<Dept>builder()
                .total(total)
                .rows(rows)
                .build();
    }*/
    //@PathVariable("id") 作用从路径变量/depts/{id}中获取名字叫id路径变量值赋值给方法的形参id
    @Override
    //public Result getById(@PathVariable("id") Integer deptId){
    public PageResult<Dept> page(Integer page,Integer pageSize,String name,Integer status){
        //开始分页
        PageHelper.startPage(page,pageSize);
        //执行查询
        List<Dept> list=deptMapper.list(name,status);
        //数据查询    条件查询列表强转为Page类型
        Page<Dept> rs = (Page<Dept>) list;
        //数据封装
        System.out.println("数据封装");
        return PageResult.<Dept>builder()
                .total(Long.valueOf(rs.getTotal()).intValue())
                .rows(rs.getResult())
                .build();
    }

    @Override
    public Dept getById(Integer deptId) {
        Dept dept = deptMapper.selectById(deptId);
        return dept;
    }

    @Override
    public void update(Dept dept) {
        deptMapper.update(dept);
    }

    @Override
    public void deleteById(Integer id) {
        deptMapper.deleteById(id);
    }

    @Override
    public List<Dept> getAllDepts() {
        return deptMapper.getAllDepts();
    }
}